//
//  AppDelegate.m
//  Philippe
//
//  Created by Maximilien Servajean on 04/05/14.
//  Copyright (c) 2014 Maximilien Servajean. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize sonde;
@synthesize matrice;
@synthesize activation;
@synthesize similarity;
@synthesize echo;
@synthesize slider;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    m = [[Matrix alloc]init];
    id windowDelegate = self;
    [_window setDelegate:windowDelegate];
}

- (IBAction)calcule:(id)sender {
    
    NSString* _sondeString = [sonde stringValue];



    NSMutableString* sondeString = [_sondeString mutableCopy];
    
    if ([sondeString isEqualToString:@""]) {
        
    } else {
        [self calculeSonde:sondeString];
    }
    
//    [matriceDelegate setBottom:1];
}

- (void)calculeSonde:(NSMutableString*)sondeString {
    for(NSInteger i = 0 ; i < [sondeString length] ; i++) {
        if ([sondeString characterAtIndex:i]==',') {
            [sondeString replaceCharactersInRange:NSMakeRange(i,i) withString:@"."];
        }
    }
    
    NSArray* sondeElmt = [sondeString componentsSeparatedByString:@";"];
    
    
    
    NSMutableArray* sondeArray = [[NSMutableArray alloc]init];
    for (NSInteger i = 0; i < [sondeElmt count]; i++) {
        
        
        NSNumber *num = @([[sondeElmt objectAtIndex:i] doubleValue]);
        [sondeArray addObject:num];
    }
    
    
    if(![m addSonde:sondeArray]){
        [echo setStringValue:[NSString stringWithFormat:@"La sonde '%@' ne respecte pas le format ou les tailles précédemment utilisées...",sondeString]];
    } else {
        [similarity setString:[m similarityString]];
        [activation setString:[m activationString]];
        [matrice setString:[m matriceString]];
        [echo setStringValue:[m echoString]];
        [matrice scrollToEndOfDocument:self];
        [activation scrollToEndOfDocument:self];
        [similarity scrollToEndOfDocument:self];
        
    }
}

- (IBAction)clear:(id)sender {
    [m clear];
    [similarity setString:@""];
    [activation setString:@""];
    [matrice setString:@""];
    [echo setStringValue:@""];
    [sonde setStringValue:@""];
}

- (void)performCalcul:(id)inParam {
    NSString* pathFile = (NSString*)inParam;
    DDFileReader * reader = [[DDFileReader alloc] initWithFilePath:pathFile];
    
    unsigned int breakDuration = [slider intValue];
    NSLog(@"%d = %d",breakDuration,[slider intValue]);
    NSString * line = nil;
    while ((line = [reader readLine])) {
        NSMutableString* uneSondeString;
        if ([line characterAtIndex:[line length]-1]=='\n')
            uneSondeString = [NSMutableString stringWithString:[line substringToIndex:[line length]-1]];
        else
            uneSondeString = [NSMutableString stringWithString:line];

        [sonde setStringValue:uneSondeString];
        [self calculeSonde:uneSondeString];
        NSLog(@"Sleep %d",breakDuration);
        [NSThread sleepForTimeInterval:(((float)breakDuration)/(float)1000)];
    }
    
    
}

-  (IBAction)decrementFontSize:(id)sender{
    [matrice changeFontSize:-1.0];
    [activation changeFontSize:-1.0];
    [similarity changeFontSize:-1.0];
    [matrice scrollToEndOfDocument:self];
    [activation scrollToEndOfDocument:self];
    [similarity scrollToEndOfDocument:self];
}

-  (IBAction)incrementFontSize:(id)sender{
    [matrice changeFontSize:1.0];
    [activation changeFontSize:1.0];
    [similarity changeFontSize:1.0];
    [matrice scrollToEndOfDocument:self];
    [activation scrollToEndOfDocument:self];
    [similarity scrollToEndOfDocument:self];
}

- (void)windowDidResize:(NSNotification *)notification {
    [matrice scrollToEndOfDocument:self];
    [activation scrollToEndOfDocument:self];
    [similarity scrollToEndOfDocument:self];
}

-(BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
{
    [NSThread detachNewThreadSelector:@selector(performCalcul:) toTarget:self withObject:filename];
    return YES;
}

@end
